//index.js
//获取应用实例
var App = getApp()
Page({
  data: {
    motto: 'Hello World',
    merchants:{},
    prompt: {
      hidden: !0,
    },
  },
  onLoad (option) {
   
    let agent = wx.getStorageSync('agent')
    let position = wx.getStorageSync('position')
    this.setData({
      agent,
      position,
      productStatus: option.productStatus
    })
    this.onPullDownRefresh()
  },
  initData () {
    const merchants = {
      items: [],
      total: '',
      params: {
        page : 1,
        pageCount: 10
      }
    }
    this.setData({
      merchants: merchants
    })
  },
  getList (cb) {
    let {merchants, agent, position} = this.data;
    let params = merchants.params;
    let location = position.location;
    params.sellerId = agent.id;
    params.lng = location.split(',')[0];
    params.lat = location.split(',')[1];
    App.HttpService.getMerchants(params).then(res => {
      cb && cb();
      let data = res.data.data;
      console.log(data);
      merchants.items = [...merchants.items, ...data.shopList];
      if (data.shopList.length > 0) {
        params.page = params.page + 1;
        merchants.total = data.counts;
      }            
      this.setData({merchants});
    }, e => {
      cb && cb();
    })
  },
  onPullDownRefresh () {
    wx.showNavigationBarLoading()
    this.initData()
    this.getList(() => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    })
  },
  onReachBottom () {
    let merchants = this.data.merchants
    if (merchants.items.length === merchants.total) {
      return
    }
    this.getList()
  },
  toMerchant (e) {
    // let {id, name} = e.currentTarget.dataset
    // console.log(id)
  },
  booking (e) {
    // console.log(e.currentTarget.dataset)
    let {id, name} = e.currentTarget.dataset
    let {productStatus} = this.data
    if (productStatus == 0) { // 普通商品选择店铺
      wx.navigateTo({
        url: `/pages/order/confirm/index?merchantId=${id}&merchantName=${name}`
      })
    } else if (productStatus == 1) { // 拼团商品选择店铺
      wx.navigateTo({
        url: `/pages/group-buying/confirm/index?merchantId=${id}&merchantName=${name}`
      })
    } else if (productStatus == 2) { // 套餐商品选择店铺
      wx.navigateTo({
        url: `/pages/order/confirm/index?merchantId=${id}&merchantName=${name}`
      })
    }
  },
})
